Page({
  data: {
    tabList: [
      {
        title: "活动公告",
        isRead: true,
      },
      {
        title: "订单消息",
        isRead: false,
      },
    ],
    current: 0,
    messageList: [
      {
        title: "免费得iPhone11",
        content: "免费得iPhone11，快来参与活动吧！",
        time: "2023-10-01 12:00",
        isRead: false,
        type: 0,
      },
      {
        title: "免费得iPhone11",
        content: "免费得iPhone11，快来参与活动吧！",
        time: "2023-10-01 12:00",
        isRead: false,
        type: 0,
      },
      {
        title: "免费得iPhone11",
        content: "免费得iPhone11，快来参与活动吧！",
        time: "2023-10-01 12:00",
        isRead: false,
        type: 0,
      },
      {
        title: "免费得iPhone11免费得iPhone11",
        content:
          "免费得iPhone11，快来参与活动吧免费得iPhone11，快来参与活动吧！",
        time: "2023-10-01 12:00",
        isRead: true,
        type: 0,
      },
      {
        title: "订单已取消",
        content: "订单已成功取消，感谢您的关注",
        time: "2023-10-01 12:00",
        isRead: false,
        type: 1,
      },
    ],
    // 二次删除弹窗
    deleteVisible: false,
    coinData: {
      coin: 0,
      title: "",
    },
    // 租币toast
    showCoinTip: false,
    // popup
    messageVisible: false, 
  },
  onLoad() {},

  checkTab(e) {
    // 切换tab
    const { index } = e.currentTarget.dataset;
    this.setData({
      current: index,
    });
    if (index && !this.data?.tabList[index]?.isRead) {
      this.setData({
        [`tabList[${index}].isRead`]: true,
      });
    }
    console.log(this.data.tabList);
  },
  deleteRead() {
    this.setData({
      deleteVisible: true,
    });
  },

  AllRead() {
    console.log("全部已读");
    const unreadCount = this.data.messageList.filter(
      (item) => !item.isRead
    ).length;
    console.log(unreadCount);

    if (!unreadCount) return;
    this.data.messageList.forEach((item, index) => {
      this.setData({
        [`messageList[${index}].isRead`]: true,
      });
    });
    this.showCoinToast()
  },

  // 获取租币土司
  showCoinToast() {
    this.setData({
      coinData: {
        ...this.data.coinData,
        coin: 30,
        title: "阅读任务",
      },
      showCoinTip: true,
    });
    setTimeout(() => {
      this.setData({
        showCoinTip: false,
      });
    }, 2000);
  },

  handlePrimaryButtonTap() {
    this.setData({
      deleteVisible: false,
    });
  },

  handleSecondaryButtonTap() {
    this.handlePrimaryButtonTap();
    // 删除已读消息
    const newList = this.data.messageList.filter((item) => !item.isRead);

    // 更新数据
    this.setData({
      messageList: newList,
    });
  },

  onClickItem(index) {
    console.log(index);
    this.setData({
      messageVisible: true,
    });
  },

  close() {
    this.setData({
      messageVisible: false,
    });
    this.showCoinToast()
  },
});
